package com.go.where.manager.action.go;

import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;

import com.go.where.manager.action.SuperAction;
import com.go.where.manager.common.Constants;
import com.go.where.manager.page.Pagination;
import com.go.where.manager.pojo.AlbumInfo;
import com.go.where.manager.pojo.StarPhotoInfo;
import com.go.where.manager.service.AlbumService;
import com.go.where.manager.service.StarPhotoService;
import com.go.where.manager.util.CommonUtil;
import com.go.where.manager.util.CompressPicUtil;
import com.go.where.manager.util.DateUtil;

@InterceptorRefs( {
		@InterceptorRef(value = "defaultStack", params = { "maximumSize",
				"2048000", "allowedTypes",
				"image/bmp,image/png,image/gif,image/jpeg" }),
		@InterceptorRef(value = "defalutStackSet") })
public class StarPhotoAction extends SuperAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4383195694100314090L;
	private static final Logger log = Logger.getLogger(StarPhotoAction.class);

	public Pagination<?> page;

	@Resource(name = "starPhotoService")
	private StarPhotoService starPhotoService;
	@Resource(name = "albumService")
	private AlbumService albumService;

	private StarPhotoInfo starPhotoInfo;
	private StarPhotoInfo starPhotoInfoQuery;

	private List<AlbumInfo> albumList;

	private String id;
	private String elect;

	private List<File> photos;
	private List<String> photosFileName;
	private List<String> photosContentType;
	private List<String> mvUrl;

	/**
	 * 查询列表
	 * 
	 * @return
	 */
	public String list() {
		if (starPhotoInfoQuery == null) {
			starPhotoInfoQuery = new StarPhotoInfo();
			starPhotoInfoQuery.setPageSize(Constants.INIT_PAGESIZE);
			starPhotoInfoQuery.setCurrentPage(Constants.INIT_CURRENTPAGE);
		}
		try {
			starPhotoInfoQuery.setIsDelete(Constants.NOT_DELETED);
			page = starPhotoService.queryPage(starPhotoInfoQuery);
		} catch (Exception e) {
			addActionError(e.getMessage());
			log.error("查询信息异常", e);
		}
		return LIST;
	}

	public String add() {
		albumList = albumService.queryAll();
		return INPUT;
	}

	public String upload() {
		try {
			String saveDir = ServletActionContext.getServletContext()
					.getRealPath(
							CommonUtil.FILE_SEPARATOR + Constants.STAR_IMAGES);
			File dir = new File(saveDir);
			if (!dir.exists()) {
				dir.mkdirs();
			}
			List<String> filePaths = new ArrayList<String>();
			for (int i = 0; i < photos.size(); i++) {
				File file = photos.get(i);
				String fileName = photosFileName.get(i);
				String newFileName = DateUtil.format(new Date(),
						"yyyyMMddHHmmss")
						+ "_"
						+ CommonUtil.createRandomNumber()
						+ "_"
						+ fileName.substring(fileName.indexOf("."));
				String dest = saveDir + CommonUtil.FILE_SEPARATOR + newFileName;
				FileUtils.copyFile(file, new File(dest));
				CompressPicUtil.compressPicture(saveDir, newFileName, file);
				filePaths.add(CommonUtil.FILE_SEPARATOR + Constants.STAR_IMAGES
						+ Constants.ZOOM_PATH + CommonUtil.FILE_SEPARATOR
						+ newFileName);
			}
			HttpServletResponse response = ServletActionContext.getResponse();
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			// 返回图片地址，这里我们实际用的时候可能返回实际图片和缩略图，
			// 上传后真正显示的时候用缩略图,实际保存地址为原图片地址。那样加载更快
			out.write(filePaths.get(0));
			out.close();
			out.flush();
			return "json";
		} catch (Exception e) {
			log.error("上传文件过程中出错", e);
			addActionError("上传文件过程中出错");
			return ERROR;
		}
	}

	/**
	 * 保存
	 * 
	 * @return
	 */
	public String save() {
		try {
			List<StarPhotoInfo> starPhotos = new ArrayList<StarPhotoInfo>();
			for (int i = 0; i < mvUrl.size(); i++) {
				StarPhotoInfo starPhoto = new StarPhotoInfo();
				starPhoto.setPhotoPath(mvUrl.get(i));
				starPhoto.setUserId(getCurrentOperator().getUserId());
				starPhoto.setIsDelete(Constants.NOT_DELETED);
				starPhoto.setPublishTime(new Date());
				starPhoto.setAlbumId(starPhotoInfo.getAlbumId());
				starPhoto.setIsElect(starPhotoInfo.getIsElect());
				starPhoto.setElectBeginTime(starPhotoInfo.getElectBeginTime());
				starPhoto.setElectEndTime(starPhotoInfo.getElectEndTime());
				starPhoto.setIntroduction(starPhotoInfo.getIntroduction());
				starPhotos.add(starPhoto);
			}
			starPhotoService.savePhotos(starPhotos);
			redirectionUrl = "star_photo!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("保存信息异常", e);
			addActionError("保存信息异常！" + e);
			return this.add();
		}
	}

	public String setElect() {
		starPhotoInfo = new StarPhotoInfo();
		starPhotoInfo.setId(id);
		starPhotoInfo.setIsElect(elect);
		try {
			starPhotoService.update(starPhotoInfo, getCurrentOperator());
			redirectionUrl = "star_photo!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("设置为票选异常", e);
			addActionError("设置为票选异常" + e);
			return this.list();
		}
	}

	/**
	 * 假删除
	 * 
	 * @return
	 */
	public String falseDelete() {
		starPhotoInfo = new StarPhotoInfo();
		starPhotoInfo.setId(id);
		starPhotoInfo.setIsDelete(Constants.DELETED);
		try {
			starPhotoService.update(starPhotoInfo, getCurrentOperator());
			redirectionUrl = "star_photo!list.htm";
			return SUCCESS;
		} catch (Exception e) {
			log.error("删除信息异常", e);
			addActionError("删除信息异常" + e);
			return this.list();
		}
	}

	public Pagination<?> getPage() {
		return page;
	}

	public void setPage(Pagination<?> page) {
		this.page = page;
	}

	public StarPhotoInfo getStarPhotoInfo() {
		return starPhotoInfo;
	}

	public void setStarPhotoInfo(StarPhotoInfo starPhotoInfo) {
		this.starPhotoInfo = starPhotoInfo;
	}

	public StarPhotoInfo getStarPhotoInfoQuery() {
		return starPhotoInfoQuery;
	}

	public void setStarPhotoInfoQuery(StarPhotoInfo starPhotoInfoQuery) {
		this.starPhotoInfoQuery = starPhotoInfoQuery;
	}

	public List<AlbumInfo> getAlbumList() {
		return albumList;
	}

	public void setAlbumList(List<AlbumInfo> albumList) {
		this.albumList = albumList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<File> getPhotos() {
		return photos;
	}

	public void setPhotos(List<File> photos) {
		this.photos = photos;
	}

	public List<String> getPhotosFileName() {
		return photosFileName;
	}

	public void setPhotosFileName(List<String> photosFileName) {
		this.photosFileName = photosFileName;
	}

	public List<String> getPhotosContentType() {
		return photosContentType;
	}

	public void setPhotosContentType(List<String> photosContentType) {
		this.photosContentType = photosContentType;
	}

	public List<String> getMvUrl() {
		return mvUrl;
	}

	public void setMvUrl(List<String> mvUrl) {
		this.mvUrl = mvUrl;
	}

	public String getElect() {
		return elect;
	}

	public void setElect(String elect) {
		this.elect = elect;
	}

}
